﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Samples.CustomControls;

namespace ActiveStory.UI
{
    /// <summary>
    /// Interaction logic for ProjectWizard.xaml
    /// </summary>
    public partial class ProjectWizard : Window
    {
        private bool _newProject;
        public bool NewProject
        {
            get
            {
                return _newProject;
            }
        }
        private string _projectName;
        public String ProjectName
        {
            get
            {
                return _projectName;
            }
            set
            {
                _projectName = value;
            }
        }
        private string _projectFolder;
        public String ProjectFolder
        {
            get
            {
                return _projectFolder;
            }
            set
            {
                _projectFolder = value;
            }
        }
        private string _projectFile;
        public String ProjectFile
        {
            get 
            {
                return _projectFile;
            }
            set
            {
                _projectFile = value;
            }
        }
        public double DefaultCanvasWidth
        {
            get
            {
                return double.Parse(CanvasWidthTextBox.Text);
            }
        }
        public double DefaultCanvasHeight
        {
            get
            {
                return double.Parse(CanvasHeightTextBox.Text);
            }
        }
        private Color _canvasColor;
        public Color CanvasColor
        {
            get
            {
                return _canvasColor;
            }
        }
        public ProjectWizard()
        {
            InitializeComponent();
            _newProject = true;
            _projectFile = "";
            _projectName = "";
            _projectFolder = "";
            _canvasColor = Colors.White;
            CanvasColorButton.Background = new SolidColorBrush(_canvasColor);

            foreach (String item in Properties.Settings.Default.RecentProjects)
            {
                //OpenProjectFileComboBox.Items.Add(item);
                OpenProjectFileListBox.Items.Add(new ProjectListEntry(item)); //item);
            }
        }

        private void NewProjectOkButton_Click(object sender, RoutedEventArgs e)
        {
            _projectName = ProjectNameTextBox.Text;
            _projectFolder = NewProjectDirectoryTextBox.Text;
            _newProject = true;
            DialogResult = true;
        }

        private void OpenProjectOkButton_Click(object sender, RoutedEventArgs e)
        {
            _projectFile = OpenProjectFileTextBox.Text; //OpenProjectFileComboBox.Text;
            _newProject = false;
            DialogResult = true;
        }

        private void NewProjectFolderBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.ShowNewFolderButton = true;
            fbd.Description = "Choose the folder you want to save the ActiveStory Enhanced Project in:";
            System.Windows.Forms.DialogResult dr = fbd.ShowDialog();
            if (!dr.Equals(System.Windows.Forms.DialogResult.OK))
            {
                return;
            }
            NewProjectDirectoryTextBox.Text = fbd.SelectedPath;
        }

        private void OpenProjectBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "ActiveStory Enhanced Projects(*" + ActiveStoryApplication.ProjectFileExtension + ")|*" + ActiveStoryApplication.ProjectFileExtension;
            ofd.Multiselect = false;
            ofd.AddExtension = true;
            ofd.DefaultExt = ActiveStoryApplication.ProjectFileExtension;
            ofd.FilterIndex = 0;
            ofd.CheckPathExists = true;
            ofd.ValidateNames = true;
            ofd.Title = "Select an ActiveStory Enhanced Project File";
            if ((bool)ofd.ShowDialog(this) != true)
            {
                return;
            }
            //OpenProjectFileComboBox.Text = ofd.FileName;
            OpenProjectFileTextBox.Text = ofd.FileName;
        }

        private void CanvasColorButton_Click(object sender, RoutedEventArgs e)
        {
            ColorPickerDialog cpd = new ColorPickerDialog();
            cpd.StartingColor = _canvasColor;
            cpd.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            cpd.Height = cpd.Height + 20;
            cpd.Owner = this;
            if ((bool)cpd.ShowDialog() != true)
                return;
            _canvasColor = cpd.SelectedColor;
            CanvasColorButton.Background = new SolidColorBrush(_canvasColor);
        }

        private void OpenProjectFileListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (OpenProjectFileListBox.SelectedIndex < 0)
            {
                return;
            }
            OpenProjectFileTextBox.Text = ((ProjectListEntry)OpenProjectFileListBox.SelectedItem).ProjectPath;//.ToString();
        }
        class ProjectListEntry
        {
            public String ProjectName { get; set; }
            public String ProjectPath { get; set; }
            public ProjectListEntry(String projectPath)
            {
                ProjectPath = projectPath;
                ProjectName = System.IO.Path.GetFileNameWithoutExtension(projectPath);
            }
        }

    }
}
